********************************************************************************
*** Appendix Table: Implied Cost of Credit by Forgoing Bundle Discount

use "data/pseudodata/userlevel_pseudodata.dta", clear
*use "data/cleaned/collapsed action with credit.dta", clear

label variable bundle "Bundle Discount"

* Define utilization rate to get loan duration (defined at the person-level then averaged)
gen shareins = totdayscoverage90/daysnonzeroreserve90 if daily==1
summ shareins if daily==1, det
gen util_res = `r(mean)'

* Calculate implied cost of credit for each bundle purchase size and output table
gen paydays = paydays1 if bundle == 1
	replace paydays = 14 if paydays1 == 12
	replace paydays = 30 if paydays1 == 24
gen aprto14 = .
gen aprto30 = . 
	replace aprto14 = (((2*dayrate)/(dayrate*(12-paydays)))/((14-paydays)/util_res))*365*100
	replace aprto30 = (((6*dayrate)/(dayrate*(24-paydays)))/((30-paydays)/util_res))*365*100
	replace aprto30 = (((4*dayrate)/(dayrate*(24-12)))/((30-paydays)/util_res))*365*100 if paydays == 14
	replace aprto14 = 0 if paydays1 >= 12 & bundle == 1
	replace aprto30 = 0 if paydays1 >= 24 & bundle == 1

	label variable aprto14 "APR Implied by Forgoing 14-Day Bundle (\%)"
	label variable aprto30 "APR Implied by Forgoing 30-Day Bundle (\%)"

	eststo clear

	eststo est1: estpost tabstat aprto14 aprto30, by(paydays) listwise ///
		statistics(mean sd) columns(statistics) nototal
		
	esttab using "output/tables/implied costs by bundle size.tex", ///
		mtitle("Days Purchased") nonumbers ///
		noobs replace label unstack ///
		cells(mean(fmt(%9.0fc) label("\phantom{}\vspace{-.9mm}")))


****
* Appendix Table: ITT Effect of Bundle Discount Treatment on Quantities of Days Purchased
eststo clear

gen nonfirstnum3 = num3
gen nonfirstnum7 = num7
gen nonfirstnum14 = num14
gen nonfirstnum30 = num30

replace num3 = num3 + 1 if paydays1 == 3
replace num7 = num7 + 1 if paydays1 == 7
replace num14 = num14 + 1 if paydays1 == 14 | paydays1 == 12
replace num30 = num30 + 1 if paydays1 == 30 | paydays1 == 24
*assert num3+num7+num14+num30 == payments if daily == 1 & bound == 1 // assertion is false in pseudodata
gen num37 = num3 + num7

gen days = 3*num3 + 7*num7 + 14*num14 + 30*num30 if daily==1 & bound==1

foreach i of numlist 3 7 14 30 {
	gen dnum`i' = `i'*num`i'
}
gen dnum37 = dnum3 + dnum7

foreach var of varlist num3 num7 num14 num30 num37 {
	gen frac`var' = `var'/payments*100
	gen fracd`var' = d`var'/days*100
}

gen firstnum3 = paydays1==3 if !mi(paydays1)
gen firstnum7 = paydays1==7 if !mi(paydays1)
gen firstnum14 = paydays1==14 | paydays1==12 if !mi(paydays1)
gen firstnum30 = paydays1==30 | paydays1==24 if !mi(paydays1)

foreach var of varlist firstnum* {
	gen frac`var' = `var'/1*100
}
foreach var of varlist nonfirstnum* {
	replace `var' = . if bound==0
	gen frac`var' = `var'/(payments-1)*100
}
gen nonfirstbund = nonfirstnum14+nonfirstnum30
gen fracnonfirstbund = fracnonfirstnum14+fracnonfirstnum30

gen firstbund = firstnum14+firstnum30
gen fracfirstbund = fracfirstnum14+fracfirstnum30

gen bund = num14+num30
gen fracbund = bund/payments*100

gen nonfirstdays = days-paydays1
drop fracd*

foreach i of numlist 3 7 14 30 {
	gen dnonfirstnum`i' = `i'*nonfirstnum`i'
	gen dfirstnum`i' = `i'*firstnum`i'
}

foreach var of varlist dfirstnum* {
	gen frac`var' = `var'/paydays1*100
}

foreach var of varlist dnonfirstnum* {
	replace `var' = . if bound==0
	gen frac`var' = `var'/(nonfirstdays)*100
}

foreach var of varlist dnum* {
	gen frac`var' = `var'/(days)*100
}

gen dnonfirstbund = dnonfirstnum14+dnonfirstnum30
gen fracdnonfirstbund = fracdnonfirstnum14+fracdnonfirstnum30

gen dfirstbund = dfirstnum14+dfirstnum30
gen fracdfirstbund = fracdfirstnum14+fracdfirstnum30

gen dbund = dnum14+dnum30
gen fracdbund = dbund/days*100

local outcomes = "dnum3 dnum7 dnum14 dnum30 dbund  dfirstnum3 dfirstnum7 dfirstnum14 dfirstnum30 dfirstbund  dnonfirstnum3 dnonfirstnum7 dnonfirstnum14 dnonfirstnum30 dnonfirstbund"

gen one = paydays1 if !mi(payments)
gen dmin1 = days-paydays1 if !mi(payments) & payments!=0

label variable fracdnum3 "Fraction 3-Day"
label variable fracdnum7 "Fraction 7-Day"
label variable fracdnum14 "Fraction 14-Day"
label variable fracdnum30 "Fraction 30-Day"
label variable fracbund "Fraction Bundle"
label variable fracdbund "Fraction Bundle"

local i=1		
foreach var of varlist `outcomes' {
	eststo est`i': reg frac`var' bundle if daily==1, r
	summ frac`var' if daily==1
	estadd scalar mean = r(mean)
	if `i'<6 summ days if daily==1
	if `i'>=6 & `i'<11 qui summ one if daily==1
	if `i'>=11 summ dmin1 if daily==1
	estadd scalar sum = r(sum)
	local i=`i'+1
}

esttab est1 est2 est3 est4 est5 using "output/tables/ITT bundle frac panels days.tex", ///
	prehead("\begin{tabular}{l*{5}{c}} \hline\hline") ///
	posthead("\hline \\ \multicolumn{6}{c}{\textbf{Panel A: All Purchases}} \\\\[-1ex]") ///
	fragment ///
	replace label nomtitle unstack  ///
	prefoot("\hline") postfoot("\hline") ///
	stats(N mean sum, fmt(%11.0gc 2 %11.0gc) labels("Users" "Mean Share" "Days Purchased")) ///
	cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
		p(par([ ]) fmt(3) label("}\vspace{-.9mm}"))) ///
		mgroups("3-Day" "7-Day" "14-Day" "30-Day" "Either Bundle", ///
		pattern(1 1 1 1 1))

esttab est6 est7 est8 est9 est10 using "output/tables/ITT bundle frac panels days.tex", ///
	posthead("\hline \\ \multicolumn{6}{c}{\textbf{Panel B: First Purchase}} \\\\[-1ex]") ///
	fragment append ///
	nomtitles nonumbers nolines ///
	prefoot("\hline") postfoot("\hline") ///
	label unstack ///
	stats(N mean sum, fmt(%11.0gc 2 %11.0gc) labels("Users" "Mean Share" "Days Purchased")) ///
	cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
		p(par([ ]) fmt(3) label("}\vspace{-.9mm}")))

esttab est11 est12 est13 est14 est15 using "output/tables/ITT bundle frac panels days.tex", ///
	posthead("\hline \\ \multicolumn{6}{c}{\textbf{Panel C: Follow-On Purchases}} \\\\[-1ex]") ///
	fragment append ///
	nomtitles nonumbers  nolines ///
	prefoot("\hline") postfoot("\hline\hline \end{tabular}") ///
	label unstack ///
	stats(N mean sum, fmt(%11.0gc 2 %11.0gc) labels("Users" "Mean Share" "Days Purchased")) ///
	cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
		p(par([ ]) fmt(3) label("}\vspace{-.9mm}")))
